﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace TheRaces
{
    public partial class Form1 : Form
    {
        private readonly Guy[] _guys = new Guy[3];
        private readonly Greyhound[] _greyhounds = new Greyhound[4];
        private readonly Random _myRandom = new Random();
        public Form1()
        {
            InitializeComponent();
            CreateDogs();
            CreateGuys();
        }

        private void btnStartRace_Click(object sender, EventArgs e)
        {
            btnPlaceBet.Enabled = false;
            raceTimer.Start();
        }
        /// <summary>
        /// Adds the dogs to the Array
        /// </summary>
        private void CreateDogs()
        {
            _greyhounds[0] = new Greyhound
            {
                Name = "Hound 1",
                MyPictureBox = Greyhound1,
                StartingPosition = Greyhound1.Left,
                RacetrackLength = racetrackPictureBox.Width - Greyhound1.Width,
                Randomizer = _myRandom
            };

            _greyhounds[1] = new Greyhound
            {
                Name = "Hound 2",
                MyPictureBox = Greyhound2,
                StartingPosition = Greyhound2.Left,
                RacetrackLength = racetrackPictureBox.Width - Greyhound2.Width,
                Randomizer = _myRandom
            };

            _greyhounds[2] = new Greyhound
            {
                Name = "Hound 3",
                MyPictureBox = Greyhound3,
                StartingPosition = Greyhound3.Left,
                RacetrackLength = racetrackPictureBox.Width - Greyhound3.Width,
                Randomizer = _myRandom
            };

            _greyhounds[3] = new Greyhound
            {
                Name = "Hound 4",
                MyPictureBox = Greyhound4,
                StartingPosition = Greyhound4.Left,
                RacetrackLength = racetrackPictureBox.Width - Greyhound4.Width,
                Randomizer = _myRandom
            };
        }
        /// <summary>
        /// Creates the guys in the array
        /// </summary>
        private void CreateGuys()
        {
            _guys[0] = new Guy
            {
                Name = "Joe",
                Cash = 50,
                MyLabel = lblJoeBet,
                MyRadioButton = joeRadioBtn,
                MyNameLabel = lblBettorName,
                MyBet = new Bet()
            };

            _guys[1] = new Guy
            {
                Name = "Bob",
                Cash = 75,
                MyLabel = lblBobBet,
                MyRadioButton = bobRadioBtn,
                MyNameLabel = lblBettorName,
                MyBet = new Bet()
            };

            _guys[2] = new Guy
            {
                Name = "Al",
                Cash = 45,
                MyLabel = lblAlBet,
                MyRadioButton = alRadioBtn,
                MyNameLabel = lblBettorName,
                MyBet = new Bet()
            };

            foreach (var guy in _guys)
            {
                guy.MyBet.Bettor = guy;
                guy.MyRadioButton.CheckedChanged += guy.UpdateLabels;
            }
        }

        private void raceTimer_Tick(object sender, EventArgs e)
        {
            for(int i = 0; i <_greyhounds.Length; i++)
            {
                if(_greyhounds[i].Run())
                {
                    raceTimer.Stop();
                    foreach (var guy in _guys)
                    {
                        guy.Collect(i + 1);
                    }
                    var result = MessageBox.Show(string.Format("{0} won the race", _greyhounds[i].Name), @"Winner", MessageBoxButtons.OK);
                    if (result == DialogResult.OK)
                    {
                        ResetRace();
                    }
                }
            }
        }
        /// <summary>
        /// Resets the race
        /// </summary>
        private void ResetRace()
        {
            foreach (var greyhound in _greyhounds.Where(greyhound => greyhound != null))
            {
                greyhound.TakeStartingPosition();
            }
            btnPlaceBet.Enabled = true;
        }

        private void btnPlaceBet_Click(object sender, EventArgs e)
        {
            foreach (var guy in _guys)
            {
                if (lblBettorName.Text == guy.Name)
                {
                    var dog = Convert.ToInt32(numDog.Value);
                    var amount = Convert.ToInt32(numBetAmount.Value);
                    if (! guy.PlaceBet(dog, amount))
                    {
                        MessageBox.Show(string.Format("{0} can not cover the bet.", guy.Name));
                    }
                    guy.UpdateLabels();
                }
            }
        }
    }
}
